/* store the direction and magnitude of wind at every point in the simulation*/
import java.util.Random;
public class WindMap
{
  static loc[][] wmap;// the internal representaion of the wind map
  
  public static void initalize()
  {
   wmap = new loc[SimulationConstants.simsize][SimulationConstants.simsize]; 
  UpdateWind();
  }
  
  public static loc[][] GetWindData()
  {//returns the raw wind data to the renderer
    return wmap;
  }
public static void SetWindData(loc[][] map) {
    wmap = map;
  }
  public static void UpdateWind()
  {//this function calculates a new wind matrix and effects the TempMap and HumidMap accordingly
    Magic_Eight_Ball.prove_WindMap_Updates();
    Random R = new Random();//this needs just a little bit of noise, or else it stagnates
    int noise = 1;
    int[][] Tmap = TempMap.GetTempData();//Get temp data
    int[][] Hmap = HumidMap.GetHumidData();//get humidity data
    //calculate the gradients
    loc[][] Tgrad = gradient(Tmap);//take temperature gradient
    loc[][] Hgrad = gradient(Hmap);//take humidity gradient
    loc[][] Mgrad = gradient(HeightMap.GetHeightMap());
    //take the cross products
    //store in wmap
    for(int x=0; x<SimulationConstants.simsize; x++)
    {
      for(int y=0; y<SimulationConstants.simsize; y++)
      {
        wmap[x][y] = new loc((Tgrad[x][y].y-Hgrad[x][y].y)*-Mgrad[x][y].y+R.nextInt(noise*2)-noise,(Tgrad[x][y].x-Hgrad[x][y].x)*Mgrad[x][y].x+R.nextInt(noise*2)-noise);
      }
    }
    
    //now to modify Temp and Humid
    HumidMap.ModifyHumid(wmap);
    TempMap.ModifyTemp(wmap);
    PlantMap.UpdatePlants();
    
    
  }
  public static loc[][] gradient (int[][] A)
{
    int GRADIENTDEVIDERCONSTANT = 10;//this makes the gradients smaller, they are too big
    loc out[][] = new loc[SimulationConstants.simsize][SimulationConstants.simsize];
  for(int x=0;x<SimulationConstants.simsize;x++)
  {
    for(int y=0;y<SimulationConstants.simsize;y++)
    {
      out[x][y] = new loc(A[x][y]-A[(x==SimulationConstants.simsize-1)?0:x+1][y],A[x][y]-A[x][(y==SimulationConstants.simsize-1)?0:y+1]);
      out[x][y].x /= GRADIENTDEVIDERCONSTANT;
      out[x][y].y /= GRADIENTDEVIDERCONSTANT;
    }
    
    
  }
  return out;
      
}
}

